using UnityEngine;
using System.Collections;

public class ComponentBullet : MonoBehaviour
{
	[SerializeField]
	protected int BulletDamage = 1;
	
	const float FiringRange = 20.0f;
	protected int ScoresAwarded = 100;
	protected float BulletSpeed = 30.0f;
	
	
	// Update is called once per frame
	void Update ()
	{
		//Moving the game Object.
		transform.Translate(Vector3.up * BulletSpeed * Time.deltaTime);
		
		//If the object go beyond the FiringRange, destroy it.
		if (transform.position.x > FiringRange || transform.position.x < -FiringRange || transform.position.y > FiringRange || transform.position.y < -FiringRange)	
		{
			destroy();	
		}
	}
	
	void OnCollisionEnter(Collision collision)
	{
		destroy();
		
		//Reduce the Health of hit Asteroid 
		ContactPoint contact = collision.contacts[0];
		ComponentHealth otherHealth = contact.otherCollider.gameObject.GetComponent<ComponentHealth>();
		if (otherHealth != null)
		{
			otherHealth.Modify(-BulletDamage);
		}
		
		//add player's score
		ComponentScore Score = GetComponent<ComponentScore>();
		if (Score == null ) print("!!!");
		if (Score != null)
		{
			Score.Modify(ScoresAwarded);
		}
	}

	public void destroy()
	{
		Destroy (gameObject);
	}
	
	/*void OnGUI()
	{
		GUI.Label(new Rect(10,10,200,50),"Score: " + playerScore);
	}*/
	
}
